package br.com.financas.repository;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.financas.abstractions.ITipoRepository;
import br.com.financas.entity.Tipo;
import br.com.financas.enumflags.EnumReferenciaFlag;

public class TipoRepository implements ITipoRepository<Tipo> {
	private EntityManager manager;
	private List<Tipo> tipoList;
	
	public TipoRepository (EntityManager manager){
		this.manager = manager;
		tipoList =  new ArrayList<Tipo>();
	}

	@Override
	public void inserir(Tipo entity) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean deletar(Tipo entity) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void atualizar(Tipo entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Tipo> obterTodos() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tipo> obterPorReferencia(EnumReferenciaFlag referencia) {
		Query query;

		if (this.tipoList.size() == 0) {
			try {
				query = manager.createQuery("SELECT t FROM Tipo t WHERE t.referencia = :referencia");
				query.setParameter("referencia", referencia);
				tipoList = query.getResultList();
			} catch (Exception e) {
				manager.getTransaction().rollback();
			}
		}
		return tipoList;
	}

}
